package com.example.test622.detail

import android.graphics.Color
import android.os.Handler
import android.support.v4.os.IResultReceiver._Parcel
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseView
import com.example.common.mvi.BaseViewModel
import com.example.net.entity.SimpleVideoEntity
import com.example.test622.MyBaseCache
import com.example.test622.MyBaseCahe
import com.example.test622.databinding.ActivityVideoBinding
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.launch
import master.flame.danmaku.controller.DrawHandler
import master.flame.danmaku.danmaku.model.BaseDanmaku
import master.flame.danmaku.danmaku.model.Danmaku
import master.flame.danmaku.danmaku.model.DanmakuTimer
import master.flame.danmaku.danmaku.model.IDanmakus
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer
import master.flame.danmaku.danmaku.model.android.DanmakuContext
import master.flame.danmaku.danmaku.model.android.Danmakus
import master.flame.danmaku.danmaku.model.android.DrawingCache
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser
import master.flame.danmaku.ui.widget.DanmakuView

@Route(path = "/detail/video")
class VideoActivity : BaseView<ActivityVideoBinding,RecomonViewModel>() {
	@JvmField
	@Autowired
	var date : SimpleVideoEntity?=null
	private lateinit var dmcontext:DanmakuContext
	private fun getDanmaKuParse():BaseDanmakuParser{
		return object : BaseDanmakuParser(){
			override fun parse(): IDanmakus {
				return Danmakus()
			}
		}
	}
	private fun BaseCacheStufferss():BaseCacheStuffer.Proxy =
		object : BaseCacheStuffer.Proxy(){
			override fun prepareDrawing(danmaku: BaseDanmaku?, fromWorkerThread: Boolean) {
			
			}
			
			override fun releaseResource(danmaku: BaseDanmaku?) {
				danmaku?.tag = null
			}
			
		}
	override fun initView() {
		binding.videoDanmu.clear()
		binding.videoDanmu.removeAllDanmakus(true)
		initDanmu()
	}
	
	private fun initDanmu() {
		//创建弹幕上下文
		//上下文设置
		dmcontext = DanmakuContext()
		dmcontext.setDuplicateMergingEnabled(true)
			.setScrollSpeedFactor(1.2f)
			.setScaleTextSize(1.2f)
			.setCacheStuffer(MyBaseCahe(this),BaseCacheStufferss())
		if(binding.videoDanmu != null){
			val danmakuparse = getDanmaKuParse()
			binding.videoDanmu.setCallback(object : DrawHandler.Callback{
				override fun prepared() {
					binding.videoDanmu.start()
				}
				
				override fun updateTimer(timer: DanmakuTimer?) {
				
				}
				
				override fun danmakuShown(danmaku: BaseDanmaku?) {
				
				}
				
				override fun drawingFinished() {
				
				}
			})
			//弹幕上下文
			binding.videoDanmu.prepare(danmakuparse,dmcontext)
			//弹幕缓存
			binding.videoDanmu.enableDanmakuDrawingCache(true)
		}
		mHandler.postDelayed(object : Runnable{
			override fun run() {
				AddDanmaku(false,"10+谶")
				mHandler.postDelayed(this,1000)
			}
			
		},1000)
	}
	private val mHandler =  Handler()
	private fun AddDanmaku(isLive:Boolean,content:String){
		val dnamu:BaseDanmaku = dmcontext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL)
	}
	override fun initData() {
		ARouter.getInstance().inject(this)
		binding.videoGsy.setUp(date?.videopath,true,"")
		binding.videoGsy.startPlayLogic()
		lifecycleScope.launch {
			var danmakuContext = DanmakuContext.create()
	 	
			val danmaku =
				danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL)
			binding.videoDanmu.prepare(object : BaseDanmakuParser(){
				override fun parse(): IDanmakus {
					return Danmakus()
				}
			},danmakuContext)
			viewModel.state.observe(this@VideoActivity){
				when(it){
					is RecommonState.DanmuSuccess->{
						if(it.danmu.data.isNotEmpty()){
							for(item in it.danmu.data){
									danmaku.text = item.content+"a"
									danmaku.textSize = 30F
									danmaku.textColor = Color.WHITE
									danmaku.padding = 10
									danmaku.time = 800
									binding.videoDanmu.addDanmaku(danmaku)
									binding.videoDanmu.start()
								}
							
						}
					
					}
					is RecommonState.RecommonFail->{
						Toast.makeText(this@VideoActivity, "${it.error}", Toast.LENGTH_SHORT).show()
					}
				}
			}
		}
		viewModel.intent.trySend(RecommonIntent.getDanmu(0,date?.item_id?:""))
	}
}